#include "downloadHeaders/downloadviewh/downloadmanagerwidget.h"
#include "downloadHeaders/downloadviewh/downloaditemwidget.h"
#include "downloadHeaders/downloadcontrollerh/downloaditemcontroller.h"
#include "downloadHeaders/downloadviewh/historywidget.h"
#include "downloadHeaders/downloadmodelh/historyitemmodel.h"
#include "ui_downloadmanagerwidget.h"
#include "ui_popwidget.h"
#include "popwidget.h"
#include <QFileDialog>
#include <QPushButton>
#include <QGraphicsDropShadowEffect>
#include <QWebEngineDownloadRequest>
#include <QListWidgetItem>
#include <QFile>
#include "tabwidget.h"

downloadManagerWidget::downloadManagerWidget(QWidget *parent):
    QWidget(parent),
    ui(new Ui::downloadManagerWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    setShadowEffect(this);
    //setShadowEffect(ui->formFrame);
    connect(ui->setting, &QPushButton::clicked, this, &downloadManagerWidget::changeDownloadLocation);
    connect(ui->history, &QPushButton::clicked, this, &downloadManagerWidget::lookupHistory);

    ui->exit->setIcon(QIcon(":/resource/imgs/cancel.png"));
    ui->exit->setToolTip(tr("close"));
    ui->setting->setIcon(QIcon(":/resource/imgs/folder.png"));
    ui->setting->setToolTip(tr("change download dictionary"));
    ui->history->setIcon(QIcon(":/resource/imgs/settings_64px.png"));
    ui->history->setToolTip(tr("view history downloads"));

}
void downloadManagerWidget::setShadowEffect(QWidget * w)
{
    QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect(w);
    effect->setOffset(0, 0);//设置阴影距离
    effect->setColor(QColor(0,0,0,90));//设置阴影颜色
    effect->setBlurRadius(10);//设置阴影圆角
    w->setGraphicsEffect(effect);
}
void downloadManagerWidget::lookupHistory()
{
    historywidget* history = new historywidget();
//    TabWidget * tab = new TabWidget(this->parentWidget());
//    tab->createToolTab(history, "download history");


    history->show();
}
void downloadManagerWidget::changeDownloadLocation()
{
    QString file_path = QFileDialog::getExistingDirectory(this, "请选择保存路径...", "./");
    if(file_path.isEmpty())
        return;
    downloadDir = file_path + '/';
    historyitemmodel::changeDownloadDir(downloadDir);

}
void downloadManagerWidget::downloadRequested(QWebEngineDownloadRequest *download)
{
    Q_ASSERT(download && download->state() == QWebEngineDownloadRequest::DownloadRequested);

    QString path = setOrGetDownloadDir();
    download->setDownloadDirectory(QFileInfo(path).path());
    download->setDownloadFileName(download->downloadFileName());
    download->accept();

    downloaditemcontroller* newcontroller = new downloaditemcontroller(download, this);
    //存入数据库
    newcontroller->initializeModel(path, download->downloadFileName(), download->url(), QDateTime::currentDateTime(), download->totalBytes());
    downloaditemwidget *newwidgetitem = new downloaditemwidget(newcontroller, this);

    add(newwidgetitem, newcontroller);
    newwidgetitem->setGeometry(this->parentWidget()->geometry().width() + this->parentWidget()->geometry().x() - 395, 45, 340, 500);
    this->setVisible(true);

}


void downloadManagerWidget::add(downloaditemwidget *downloadWidget, downloaditemcontroller *newcontroller)
{
    //connect(newcontroller, &downloaditemcontroller::deleteClicked, this, &downloadManagerWidget::remove);

    newcontroller->widgetItem = new QListWidgetItem();
    newcontroller->widgetItem->setSizeHint(QSize(330, 55));
    ui->listWidget->insertItem(0, newcontroller->widgetItem);

    ui->listWidget->setItemWidget(newcontroller->widgetItem, downloadWidget);
    numberOfDownloads++;

    if (numberOfDownloads == 1)
        ui->NoItemLabel->hide();
}
void downloadManagerWidget::remove(downloaditemcontroller *newcontroller)
{
    ui->listWidget->removeItemWidget(newcontroller->widgetItem);
    delete newcontroller->widgetItem;
    --numberOfDownloads;
    if (numberOfDownloads == 0)
        ui->NoItemLabel->show();
}
QString downloadManagerWidget::setOrGetDownloadDir()
{
    if(!historyitemmodel::downloadDirValid())
         changeDownloadLocation();
    else
        downloadDir = historyitemmodel::getDownloadDir();
    return downloadDir;
}
downloadManagerWidget::~downloadManagerWidget()
{
    delete ui;
}
